<?php

/**
 * model_syllabus
 */

class Model_syllabus extends YOU {

	public function __construct()
	{
		parent::__construct();
	}

	/** 读取配置信息 **/
	public function getSet()
	{
		$set_data = $this->db->fetchRow("SELECT * FROM `tbl_syllabus_set` WHERE set_id='1' ");
		$set_data['set_week'] = unserialize($set_data['set_week']);
		$set_data['set_free'] = unserialize($set_data['set_free']);
		return $set_data;
	}

	/** 读取当前boxid **/
	public function getBox($semester_id, $class_id = 0, $syllabus_double = 0)
	{
		$where = " WHERE semester_id='{$semester_id}' ";

		if (!empty($class_id)){
			$where .= " AND class_id='{$class_id}' ";
		}

		if (!empty($syllabus_double)){
			$where .= " AND syllabus_double='{$syllabus_double}' ";
		}


		$row = $this->db->fetchRow("SELECT box_id FROM `tbl_syllabus` {$where} ORDER BY box_id DESC");
		$box_id = (int)$row['box_id'];

		return $box_id;
	}

	/** 根据boxid得到列表 **/
	public function getAllByBox($box_id)
	{
		$syllabus_lists = $this->db->fetchAll("SELECT * FROM `tbl_syllabus` WHERE box_id='{$box_id}'");

		return $syllabus_lists;
	}

	/** 生效时间列表 **/
	public function getStartList()
	{
		$syllabus_lists = $this->db->fetchAll("SELECT box_id,start_time FROM `tbl_syllabus` ORDER BY box_id ASC");
		foreach ($syllabus_lists as $key => $value){
			$data[$value['start_time']] = $value['box_id'];
		}

		return $data;
	}

	/** 添加课程表 **/
	public function add($id, $box_id, $class_id, $semester_id, $syllabus_double, $data)
	{

		// 根据id得到任课情况 staff_id subject_id class_id
		if ($id >= 0){
			$sb = $this->db->fetchRow("SELECT * FROM `tbl_staff_subject` WHERE id='{$id}'");
			if (empty($sb)){
				return  false;
			}

			$staff_id   = $sb['staff_id'];
			$subject_id = $sb['subject_id'];
			$class_id   = $class_id;
			$week_hour  = $sb['week_hour'];

			// 当前已用课时  单双周不计 group by
			$row = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_syllabus` WHERE staff_id='{$staff_id}' AND subject_id='{$subject_id}' AND class_id='{$class_id}' GROUP BY syllabus_double");
			$hour_count = (int)$row['count'];

			if ($hour_count >= $week_hour){
				return false;
			}
			// 更新已用课时
			$info = array(
			"hour_count" => $hour_count,
			);
			$this->db->do_update("tbl_staff_subject", $info, "id='{$id}'");
		}else{
			$subject_id = $id;
		}

		// 添加
		$syllabus_week    = $data['syllabus_week'];
		$syllabus_section = $data['syllabus_section'];

		$info = array(
		"box_id"           => $box_id,
		"class_id"         => $class_id,
		"semester_id"      => $semester_id,
		"syllabus_double"  => $syllabus_double,
		"staff_id"         => $staff_id,
		"subject_id"       => $subject_id,
		"syllabus_week"    => $syllabus_week,
		"syllabus_section" => $syllabus_section,
		"status"           => 1,
		"start_time"       => "0000-00-00",
		"create_time"      => date("Y-m-d H:i:s"),
		"update_time"      => "0000-00-00 00:00:00"
		);
		
		
		
		$this->db->do_insert("tbl_syllabus", $info);


	}

	/** 导入添加课程表 **/
	public function in($staff_name, $subject_name, $box_id, $class_id, $semester_id, $syllabus_double, $data)
	{
		$row = $this->db->fetchRow("SELECT staff_id FROM `tbl_staff` WHERE staff_name='{$staff_name}'");
		$staff_id = $row['staff_id'];

		$row = $this->db->fetchRow("SELECT subject_id FROM `tbl_subject` WHERE subject_name='{$subject_name}'");
		$subject_id = $row['subject_id'];

		$week_hour  = 1;

		// 当前已用课时  单双周不计 group by
		$row = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_syllabus` WHERE staff_id='{$staff_id}' AND subject_id='{$subject_id}' AND class_id='{$class_id}' GROUP BY syllabus_double");
		$hour_count = (int)$row['count'];

		if ($hour_count >= $week_hour){
			//return false;
		}
		// 更新已用课时
		$info = array(
		"hour_count" => $hour_count,
		);
		$this->db->do_update("tbl_staff_subject", $info, "id='{$id}'");


		// 添加
		$syllabus_week    = $data['syllabus_week'];
		$syllabus_section = $data['syllabus_section'];

		$info = array(
		"box_id"           => $box_id,
		"class_id"         => $class_id,
		"semester_id"      => $semester_id,
		"syllabus_double"  => $syllabus_double,
		"staff_id"         => $staff_id,
		"subject_id"       => $subject_id,
		"syllabus_week"    => $syllabus_week,
		"syllabus_section" => $syllabus_section,
		"status"           => 1,
		"start_time"       => "0000-00-00",
		"create_time"      => date("Y-m-d H:i:s"),
		"update_time"      => "0000-00-00 00:00:00"
		);
		$this->db->do_insert("tbl_syllabus", $info);


	}

	/** 查询是否存在 存在更新 不存在新增 **/
	public function update($id, $class_id, $semester_id, $syllabus_double, $syllabus_week, $syllabus_section, $data)
	{
		// 根据id得到任课情况 staff_id subject_id class_id
		$sb = $this->db->fetchRow("SELECT * FROM `tbl_staff_subject` WHERE id='{$id}'");
		if (empty($sb)){
			return  false;
		}
		$staff_id   = $sb['staff_id'];
		$subject_id = $sb['subject_id'];
		$class_id   = $sb['class_id'];
		$week_hour  = $sb['week_hour'];

		// 当前已用课时  单双周不计 group by
		$row = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_syllabus` WHERE staff_id='{$staff_id}' AND subject_id='{$subject_id}' AND class_id='{$class_id}' GROUP BY syllabus_double");
		$hour_count = (int)$row['count'];

		if ($hour_count >= $week_hour){
			return false;
		}
		// 更新已用课时
		$info = array(
		"hour_count" => $hour_count,
		);
		$this->db->do_update("tbl_staff_subject", $info, "id='{$id}'");


		$row = $this->db->fetchRow("SELECT syllabus_id FROM `tbl_syllabus` WHERE class_id='{$class_id}' AND semester_id='{$semester_id}' AND syllabus_double='{$syllabus_double}' AND syllabus_week='{$syllabus_week}' AND syllabus_section='{$syllabus_section}'");
		if (!empty($row)){
			$info = array(
			"class_id"         => $class_id,
			"semester_id"      => $semester_id,
			"syllabus_double"  => $syllabus_double,
			"staff_id"         => $staff_id,
			"subject_id"       => $subject_id,
			"syllabus_week"    => $syllabus_week,
			"syllabus_section" => $syllabus_section,
			"start_time"       => $data['start_time'],
			"update_time"      => date("Y-m-d H:i:s"),
			);
			$this->db->do_update("tbl_syllabus", $info, "syllabus_id='{$row['syllabus_id']}'");

		}else{
			$info = array(
			"class_id"         => $class_id,
			"semester_id"      => $semester_id,
			"syllabus_double"  => $syllabus_double,
			"staff_id"         => $staff_id,
			"subject_id"       => $subject_id,
			"syllabus_week"    => $syllabus_week,
			"syllabus_section" => $syllabus_section,
			"status"           => 1,
			"start_time"       => $data['start_time'],
			"create_time"      => date("Y-m-d H:i:s"),
			"update_time"      => "0000-00-00 00:00:00"
			);
			$this->db->do_insert("tbl_syllabus", $info);


		}
	}

}



?>